<?php

defined('_JEXEC') or die;

class ExercisesViewExercises extends JViewLegacy
{
    
        
	protected $categories;
	protected $items;
	protected $pagination;
	protected $state;

	/**
	 * Method to display the view.
	 *
	 * @param   string  $tpl  A template file to load. [optional]
	 *
	 * @return  mixed  A string if successful, otherwise a JError object.
	 *
	 * @since   1.6
	 */
	public function display($tpl = null)
	{
		// Initialise variables.
                JHtml::addIncludePath(JPATH_COMPONENT . '/helpers/html');
                JHtml::_('exercise.stylesheet');
                JHtml::_('exercise.jquery');
		$this->categories	= $this->get('CategoryOrders');
		$this->items		= $this->get('Items');
		$this->pagination	= $this->get('Pagination');
		$this->state		= $this->get('State');
                $this->filterForm    = $this->get('FilterForm');
		// Check for errors.
		if (count($errors = $this->get('Errors')))
		{
			JError::raiseError(500, implode("\n", $errors));
			return false;
		}
                ExercisesHelper::addSubmenu('exercises');
		$this->addToolbar();
                JHtml::addIncludePath(JPATH_COMPONENT.'/helpers/html');
                JHtml::_('exercise.stylesheet');
                $this->sidebar = JHtmlSidebar::render();
		parent::display($tpl);
	}

	/**
	 * Add the page title and toolbar.
	 *
	 * @return  void
	 *
	 * @since   1.6
	 */
	protected function addToolbar()
	{
		require_once JPATH_COMPONENT . '/helpers/exercises.php';

		$canDo = ExercisesHelper::getActions($this->state->get('filter.category_id'));
		$user = JFactory::getUser();
		JToolBarHelper::title(JText::_('COM_EXERCISES_MANAGER_EXERCISES'), 'exercises.png');
		if (($canDo->get('core.create')))
		{
			JToolBarHelper::addNew('exercise.add');
		}

		if (($canDo->get('core.edit')))
		{
			JToolBarHelper::editList('exercise.edit');
		}

		if ($canDo->get('core.edit.state'))
		{
			if ($this->state->get('filter.state') != 2)
			{
				JToolBarHelper::divider();
				JToolBarHelper::publish('exercises.publish', 'JTOOLBAR_PUBLISH', true);
				JToolBarHelper::unpublish('exercises.unpublish', 'JTOOLBAR_UNPUBLISH', true);
			}

			if ($this->state->get('filter.state') != -1)
			{
				JToolBarHelper::divider();
				if ($this->state->get('filter.state') != 2)
				{
					JToolBarHelper::archiveList('exercises.archive');
				}
				elseif ($this->state->get('filter.state') == 2)
				{
					JToolBarHelper::unarchiveList('exercises.publish');
				}
			}
		}

		if ($canDo->get('core.edit.state'))
		{
			JToolBarHelper::checkin('exercises.checkin');
		}

		if ($this->state->get('filter.state') == -2 && $canDo->get('core.delete'))
		{
			JToolBarHelper::deleteList('', 'exercises.delete', 'JTOOLBAR_EMPTY_TRASH');
			JToolBarHelper::divider();
		}
		elseif ($canDo->get('core.edit.state'))
		{
			JToolBarHelper::trash('exercises.trash');
			JToolBarHelper::divider();
		}

		if ($canDo->get('core.admin'))
		{
			JToolBarHelper::preferences('com_exercises');
			JToolBarHelper::divider();
		}
		JToolBarHelper::help('JHELP_COMPONENTS_EXERCISES_EXERCISES');
	}
        protected function getSortFields()
	{
		return array(
			'ordering' => JText::_('JGRID_HEADING_ORDERING'),
			'a.state' => JText::_('JSTATUS'),
			'a.name' => JText::_('COM_EXERCISES_HEADING_NAME'),
			'a.sticky' => JText::_('COM_EXERCISES_HEADING_STICKY'),
			'a.id' => JText::_('JGRID_HEADING_ID')
		);
	}
}
